local sk__xianzhu = fk.CreateSkill {

  name = "sk__xianzhu",

  tags = {},

}



sk__xianzhu:addEffect(fk.HpRecover, {
  name = "sk__xianzhu",
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__xianzhu.name) then
      event:setCostData(self, target)
      return not target.dead
    end
  end,
  on_trigger = function(self, event, target, player, data)
    event:setCostData(self, target.id)
    self:doCost(event, target, player, data)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__xianzhu.name, nil,
      "#sk__xianzhu-invoke::" .. event:getCostData(self))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { event:getCostData(self) })
    room:getPlayerById(event:getCostData(self)):drawCards(2, sk__xianzhu.name)
  end,
})
sk__xianzhu:addEffect(fk.AfterCardsMove, {
  name = "sk__xianzhu",
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__xianzhu.name) then
      for _, move in ipairs(data) do
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and not player.room:getPlayerById(move.from).dead then
            return true
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local targets = {}
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.PlayerEquip then
          table.insertIfNeed(targets, move.from)
        end
      end
    end
    player.room:sortByAction(targets)
    for _, id in ipairs(targets) do
      if not player:hasSkill(sk__xianzhu.name) then return end
      if not id.dead then
        event:setCostData(self, id.id)
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__xianzhu.name, nil,
      "#sk__xianzhu-invoke::" .. event:getCostData(self))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { event:getCostData(self) })
    room:getPlayerById(event:getCostData(self)):drawCards(2, sk__xianzhu.name)
  end,
})

return sk__xianzhu
